home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / jpl_c.zip / RATFUN.C < prev    next >
Text File  |  1986-05-18  |  1KB  |  46 lines

  1. /* 1.0  04-27-84 */
  2. /************************************************************************
  3.  *            Robert C. Tausworthe                *
  4.  *            Jet Propulsion Laboratory            *
  5.  *            Pasadena, CA 91009        1984        *
  6.  ************************************************************************/
  7.  
  8. #include "defs.h"
  9. #include "stdtyp.h"
  10. #include "errno.h"
  11. #include "mathcons.h"
  12.  
  13. /************************************************************************/
  14.     double
  15. ratfun(x, P, Q, n, m)    /* return value of rational function P(x)/Q(x)
  16.                where n = deg(P), m = deg(Q)            */
  17. /*----------------------------------------------------------------------*/
  18. double x, P[], Q[];
  19. int n, m;
  20. {
  21.     double num, den;
  22.     int abs(), chrstc();
  23.  
  24.     if ( P[n] IS 1.0 AND n > 0)
  25.         num = x + P[--n];
  26.     else
  27.         num = P[n];
  28.     for (n--; n >= 0; n--)
  29.         num = num * x + P[n];
  30.     if (Q[m] IS 1.0 AND m > 0)
  31.         den = x + Q[--m];
  32.     else
  33.         den = Q[m];
  34.     for (m--; m >= 0; m--)
  35.         den = den * x + Q[m];
  36.     if (num IS 0.0 AND den ISNT 0.0)
  37.             return 0.0;
  38.  
  39.     if (den IS 0.0 OR abs(chrstc(num) - chrstc(den)) >= MAXEXP)
  40.     {    errno = ERANGE;
  41.         return (num < 0.0 ? -INFINITY : INFINITY);
  42.     }
  43.     errno = 0;
  44.     return (num/den);
  45. }
  46.